﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.ComponentModel.Composition;

namespace Steve.UI.Extensions
{
    public sealed class SearchEngineReferrerInfo
    {
        public string AuthorityPattern { get; set; }
        public string QueryStringKey { get; set; }
    }

    [Export(typeof(ISiteModuleExtension))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public sealed class SearchReferrerSiteModuleExtension : ISiteModuleExtension
    {

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "query")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "engine")]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "ctx")]
        private static void HandleSearchReferer(HttpContextBase ctx, SearchEngineReferrerInfo engine, string query)
        {
            //log to db, cookie with campaign, etc...
            //var words = query.Split(' ');

            throw new NotImplementedException(); 
        }
        
        private static IEnumerable<SearchEngineReferrerInfo> GetEngines()
        {
            //TODO: move this to the db...
            return new SearchEngineReferrerInfo[] {
                new SearchEngineReferrerInfo() { AuthorityPattern = "www.google.", QueryStringKey = "q" },
                new SearchEngineReferrerInfo() { AuthorityPattern = "search.yahoo.com", QueryStringKey = "p" },
                new SearchEngineReferrerInfo() { AuthorityPattern = "bing.com", QueryStringKey = "q" },
                new SearchEngineReferrerInfo() { AuthorityPattern = "ask.com", QueryStringKey = "q" },
                new SearchEngineReferrerInfo() { AuthorityPattern = ".aol.de", QueryStringKey = "q" },
                new SearchEngineReferrerInfo() { AuthorityPattern = ".aol.", QueryStringKey = "query" },
                new SearchEngineReferrerInfo() { AuthorityPattern = "altavista.com", QueryStringKey = "q" },
                new SearchEngineReferrerInfo() { AuthorityPattern = "alltheweb.com", QueryStringKey = "q" }
            };
        }

        public void ProcessBeginRequest(HttpContextBase ctx, UrlObject url)
        {
            if (ctx == null) throw new ArgumentNullException("ctx");

            var referer = ctx.Request.UrlReferrer;

            if (referer != null && referer.IsAbsoluteUri)
            {
                var engines = GetEngines();

                if (engines != null)
                {
                    foreach (var e in engines)
                    {
                        if (referer.Authority.IndexOf(e.AuthorityPattern, StringComparison.OrdinalIgnoreCase) > 0)
                        {
                            var query = ctx.Request.QueryString[e.QueryStringKey];

                            HandleSearchReferer(ctx, e, query);
                        }
                    }
                }
            }
        }

        public void ProcessRewrite(HttpContextBase ctx, RewriteResult result) { }

        public void ProcessPostAuthenticateRequest(HttpContextBase ctx) { }
    }
}
